package com.tonann.web.api;

import com.alibaba.fastjson2.JSONObject;
import com.tonann.common.core.controller.BaseController;
import com.tonann.common.core.domain.AjaxResult;
import com.tonann.web.dangerous.DangerousToken;
import com.tonann.web.dangerous.OkHttpUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("App端接口管理")
@RestController
@RequestMapping("/dangerous/api")
public class DangerousChemicalsApi extends BaseController {

    @GetMapping(value = "/getDeviceList")
    public AjaxResult getList() throws Exception {
        String token = DangerousToken.token;
        if (token == null) {
            token = DangerousToken.getToken();
        }
        if (token == null) {
            return AjaxResult.error("获取试剂柜Token失败");
        } else {
            JSONObject jsonObject = getDangerousCabinetList(token);
            if (jsonObject != null) {
                Integer errCode = (Integer) jsonObject.get("errCode");
                if (errCode != null && errCode == 200) {
                    return AjaxResult.success(jsonObject);
                } else if (errCode != null && errCode == 401) {
                    token = DangerousToken.getToken();
                    if (token == null) {
                        return AjaxResult.error("获取试剂柜Token失败");
                    } else {
                        jsonObject = getDangerousCabinetList(token);
                        if (errCode != null && errCode == 200) {
                            return AjaxResult.success(jsonObject);
                        }
                    }
                }
                return AjaxResult.error("获取数据失败");
            } else {
                return AjaxResult.error("获取数据失败");
            }
        }
    }

    private JSONObject getDangerousCabinetList(String token) throws Exception {
        String url = "https://dev.schemical.kakashine.com/openapi/cabinet/list";
        JSONObject jsonObject = OkHttpUtil.getInstance().doGet(url, token);
        return jsonObject;
    }

    @GetMapping(value = "/getDeviceDetail/{code}")
    public AjaxResult getDetail(@PathVariable(value = "code") String code) throws Exception {
        String token = DangerousToken.token;
        if (token == null) {
            token = DangerousToken.getToken();
        }
        if (token == null) {
            return AjaxResult.error("获取试剂柜Token失败");
        } else {
            JSONObject jsonObject = getDangerousCabinetDetail(token, code);
            if (jsonObject != null) {
                Integer errCode = (Integer) jsonObject.get("errCode");
                if (errCode != null && errCode == 200) {
                    return AjaxResult.success(jsonObject);
                } else if (errCode != null && errCode == 401) {
                    token = DangerousToken.getToken();
                    if (token == null) {
                        return AjaxResult.error("获取试剂柜Token失败");
                    } else {
                        jsonObject = getDangerousCabinetDetail(token, code);
                        errCode = (Integer) jsonObject.get("errCode");
                        if (errCode != null && errCode == 200) {
                            return AjaxResult.success(jsonObject);
                        }
                    }
                }
                return AjaxResult.error("获取数据失败");
            } else {
                return AjaxResult.error("获取数据失败");
            }
        }
    }

    private JSONObject getDangerousCabinetDetail(String token, String code) throws Exception {
        String url = "https://dev.schemical.kakashine.com/openapi/cabinet/" + code;
        JSONObject jsonObject = OkHttpUtil.getInstance().doGet(url, token);
        return jsonObject;
    }

    @GetMapping(value = "/getDeviceStoreDetail")
    public AjaxResult getStoreDetail(String cabinetId, String doorId) throws Exception {
        String token = DangerousToken.token;
        if (token == null) {
            token = DangerousToken.getToken();
        }
        if (token == null) {
            return AjaxResult.error("获取试剂柜Token失败");
        } else {
            JSONObject jsonObject = getDangerousCabinetStoreDetail(token, cabinetId, doorId);
            if (jsonObject != null) {
                Integer errCode = (Integer) jsonObject.get("errCode");
                if (errCode != null && errCode == 200) {
                    return AjaxResult.success(jsonObject);
                } else if (errCode != null && errCode == 401) {
                    token = DangerousToken.getToken();
                    if (token == null) {
                        return AjaxResult.error("获取试剂柜Token失败");
                    } else {
                        jsonObject = getDangerousCabinetStoreDetail(token, cabinetId, doorId);
                        if (errCode != null && errCode == 200) {
                            return AjaxResult.success(jsonObject);
                        }
                    }
                }
                return AjaxResult.error("获取数据失败");
            } else {
                return AjaxResult.error("获取数据失败");
            }
        }
    }

    private JSONObject getDangerousCabinetStoreDetail(String token, String cabinetId, String doorId) throws Exception {
        String url = "https://dev.schemical.kakashine.com/openapi/cabinet/inventory?cabinetId=" + cabinetId + "&doorId=" + doorId;
        JSONObject jsonObject = OkHttpUtil.getInstance().doGet(url, token);
        return jsonObject;
    }
}
